from pydub import AudioSegment
import subprocess
import os

def pitch_shift_with_ffmpeg(input_file, output_file, pitch_factor=2.0):
    """
    使用pydub + ffmpeg实现音调调整
    
    Args:
        input_file: 输入音频文件路径
        output_file: 输出音频文件路径
        pitch_factor: 音调倍数，2.0表示升高一倍
    """
    # 使用ffmpeg进行音调调整
    cmd = [
        'ffmpeg', '-i', input_file,
        '-af', f'asetrate=44100*{pitch_factor},aresample=44100',
        '-y',  # 覆盖输出文件
        output_file
    ]
    
    try:
        subprocess.run(cmd, check=True, capture_output=True)
        print(f"音调已升高 {pitch_factor} 倍，保存到: {output_file}")
    except subprocess.CalledProcessError as e:
        print(f"ffmpeg处理失败: {e}")
    except FileNotFoundError:
        print("错误: 未找到ffmpeg，请先安装ffmpeg")

# 使用示例
if __name__ == "__main__":
    input_file = 'meirenyu_pianduan.mp3'
    output_file = 'meirenyu_pianduan_ffmpeg_pitch.mp3'
    
    # 升高一倍音调
    pitch_shift_with_ffmpeg(input_file, output_file, pitch_factor=2.0)
